package uranium.gui.add;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JTextField;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import uranium.db.ConnectionManager;
import uranium.db.DBManager;

import java.awt.Rectangle;

public class AddNewProdDistGUI extends JDialog{
		
	public void initialize(){
			//jFrame = new JFrame();
		this.setSize(new Dimension(458, 331));
		this.setTitle("Carico Distributore");
		this.setPreferredSize(new Dimension(250, 188));
		this.setContentPane(getJContentPane());
			centro();
			this.setVisible(true);
	}
	public AddNewProdDistGUI(DBManager dbm, JFrame jFrame, ConnectionManager cm){
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		this.cm = cm;
		this.connessione = cm.getConnection();
		try{
			st = connessione.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		} catch (SQLException err5){

			err5.printStackTrace();
		}
		initialize();
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="218,125"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTextField jTextField = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField2 = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField3 = null;
	private JLabel jLabel4 = null;
	private JTextField jTextField4 = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private DBManager dbm = null;
	private JButton jButton2 = null;
	private int qtaskric = 0;
	private int qtacanc = 0;
	private Connection connessione;
	private Statement st = null;
	private ResultSet rs = null;  //  @jve:decl-index=0:
	private ConnectionManager cm;
	private boolean error1 = false;
	private boolean error2 = false;
	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Prezzo OUT");
			jLabel4.setBounds(new Rectangle(15, 132, 76, 16));
			jLabel3 = new JLabel();
			jLabel3.setText("Prezzo IN");
			jLabel3.setBounds(new Rectangle(15, 110, 63, 16));
			jLabel2 = new JLabel();
			jLabel2.setText("Quantita'");
			jLabel2.setBounds(new Rectangle(15, 88, 62, 16));
			jLabel1 = new JLabel();
			jLabel1.setText("Descrizione");
			jLabel1.setBounds(new Rectangle(15, 66, 77, 16));
			jLabel = new JLabel();
			jLabel.setText(" Codice a Barre ");
			jLabel.setBounds(new Rectangle(14, 44, 100, 16));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(250, 166));
			jPanel.add(getJTextField(), null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField1(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJTextField2(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(getJTextField3(), null);
			jPanel.add(jLabel4, null);
			jPanel.add(getJTextField4(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(200, 22));
			jTextField.setBounds(new Rectangle(140, 41, 200, 22));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(200, 22));
			jTextField1.setBounds(new Rectangle(140, 63, 200, 22));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setPreferredSize(new Dimension(200, 22));
			jTextField2.setBounds(new Rectangle(140, 85, 200, 22));
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setPreferredSize(new Dimension(200, 22));
			jTextField3.setBounds(new Rectangle(140, 107, 200, 22));
		}
		return jTextField3;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setPreferredSize(new Dimension(200, 22));
			jTextField4.setBounds(new Rectangle(140, 129, 200, 22));
		}
		return jTextField4;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Cerca");
			jButton.setBounds(new Rectangle(365, 39, 69, 26));
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						error1 = cercaSR();
						error2 = cercaC();
						if(!error1 && !error2)
							JOptionPane.showMessageDialog(jFrame, "Articolo non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					}
					catch(Exception e20){
						System.err.println(e20);
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Inserisci");
			jButton1.setBounds(new Rectangle(250, 230, 83, 26));
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						inserisci();
					}
					catch(Exception e21){
						System.err.println(e21);
					}
					
				}
			});
		}
		return jButton1;
	}
	
	/**
	 * Il metodo inserisce il nuovo oggetto all'interno del db
	 * @throws SQLException 
	 */
	
	private synchronized void inserisci(){
		boolean error = false;
		int qtap = 0;
		if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Codice a Barre non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField1.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Descrizione non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField2.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Quantita' non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		else{
			boolean error3 = false;
			for (int i = 0; i <= jTextField2.getText().length() - 1; i++){
				char c = jTextField2.getText().charAt(i);
				if ((c < '0') || (c > '9'))	{
					error = true; error3 = true;
					
				}
			}
			if(error3)
				JOptionPane.showMessageDialog(jFrame, "Campo Quantita' mal formato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
		}
		if(jTextField3.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoIN non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		else if(!uranium.utility.ControlloDati.prezzoCorretto(jTextField3.getText().trim())){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoIN mal formato (0.00)", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField4.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		else if(!uranium.utility.ControlloDati.prezzoCorretto(jTextField4.getText().trim())){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT mal formato (0.00)", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(error1)
			if(Integer.parseInt(jTextField2.getText()) > qtaskric){
				JOptionPane.showMessageDialog(jFrame, "Quantita' non disponibile", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				error = true;
			}
		if(error2)
			if(Integer.parseInt(jTextField2.getText()) > qtacanc){
				JOptionPane.showMessageDialog(jFrame, "Quantita' non disponibile", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				error = true;
			}
		
		
		if(error == false){
			String insert = null;
			String cbarre = jTextField.getText();
			String descrizione = jTextField1.getText();
			int qta = Integer.parseInt(jTextField2.getText());
			double prezzoin = Double.parseDouble(jTextField3.getText());
			double prezzout = Double.parseDouble(jTextField4.getText());
			try{
				String q = "SELECT qta FROM distributore WHERE cbarre='"+jTextField.getText()+"'";
				rs = st.executeQuery(q);
				if(rs.next()){
					qtap = rs.getInt(1);
					qta += qtap;
					insert = "UPDATE distributore SET qta='" + qta + "' WHERE cbarre='" + jTextField.getText() + "'";
					//insert = "UPDATE qta FROM distributore WHERE cbarre='"+jTextField.getText()+"'";
				}
				else{
					insert="INSERT INTO distributore (cbarre,descrizione,qta,prezzoin,prezzoout) VALUES('"+cbarre+"','"+descrizione+"','"+qta+"','"+prezzoin+"','"+prezzout+"')";	
				}
			}
			catch(Exception e41){
				e41.printStackTrace();
			}
			
			dbm.executeQuery(insert);
			update(cbarre, qta);
			this.dispose();
		}	
	}
	
	private void update(String codice, int qta1){
		try{
			String insert;
			String tabella = null;
			String colonna = "cbarre";
			int qtaf = 0;
			if(error1){
				qtaf = qtaskric;
				tabella = "skeric";
			}
			else if(error2){
				qtaf = qtacanc;
				tabella = "cancelleria";
			}
			
			if(qtaf == qta1){
				JOptionPane.showMessageDialog(jFrame, "Prodotto esaurito", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				insert = "DELETE FROM "+tabella+" WHERE "+colonna+"="+codice;
				st.executeUpdate(insert);
			}
			else{
				int qta = qtaf - qta1;
				insert = "UPDATE " + tabella + " SET qta='" + qta + "' WHERE cbarre='" + codice + "'";
				st.executeUpdate(insert);
			}
		}
		catch(Exception e46){
			e46.printStackTrace();
		}
	}
	
	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(95, 230, 83, 26));
			jButton2.setText("Annulla");
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e30){
						e30.printStackTrace();
					}
				}
			});
		}
		return jButton2;
	}
	
	private void close(){
		this.dispose();
	}
	
	private boolean cercaSR(){
		if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Codice non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
		
		}
		else{
			String query = "SELECT descrizione, qta, prezzoin, prezzoout FROM skeric WHERE cbarre ='"+jTextField.getText()+"'";
			try{
				rs = st.executeQuery(query);
				if(!rs.next()){
					//JOptionPane.showMessageDialog(jFrame, "Codice in skrc non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					return false;
				}
				else{
					jTextField1.setText((String)rs.getString(1));
					qtaskric = rs.getInt(2);
					jTextField3.setText((String)rs.getString(3));
					jTextField4.setText((String)rs.getString(4));
					
				}
			}
			catch(Exception e40){
				e40.printStackTrace();
			}
		}
		return true;
	}
	
	private boolean cercaC(){
		if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Codice non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
		}
		else{
			String query = "SELECT descrizione, qta, prezzoin, prezzoout FROM cancelleria WHERE cbarre ='"+jTextField.getText()+"'";
			try{
				rs = st.executeQuery(query);
				if(!rs.next()){
					//JOptionPane.showMessageDialog(jFrame, "Codice in canc non trovato", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					return false;
				}
				else{
					
						jTextField1.setText((String)rs.getString(1));
						qtacanc = rs.getInt(2);
						jTextField3.setText((String)rs.getString(3));
						jTextField4.setText((String)rs.getString(4));
					
				}
			}
			catch(Exception e47){
				e47.printStackTrace();
			}
		}
		return true;
	}
	
}
